#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

int maxPathSumHelper(TreeNode *root, int &g) 
{
    if (!root) {
        g = 0;
        return INT_MIN;
    }   
    
    int lg = 0, rg = 0;
    int f = INT_MIN;
    f = max(f, maxPathSumHelper(root->left, lg));
    f = max(f, maxPathSumHelper(root->right, rg));
    f = max(f, root->val + lg + rg);
    g = max(0, root->val + max(lg, rg));
    return f;
}


int maxPathSum(TreeNode *root) {
    int g = 0;
    return maxPathSumHelper(root, g); 
  //  return max(g, ans);
}

//pass all the test on 1337
int main(int argc, char **argv)
{

}
